/*
 * Object   : CNetMgr
 * Type     : Backend
 *
 * Narrator : Kamran H. Khan 
 * Email    : KAMRANHK@gmail.com
 * 
 * Copyleft : Attribution - CreativeCommons http://creativecommons.org/licenses/by-nc/3.0/
 *            This software is provided 'as-is', without any express or implied warranty.
 *            In no event will the author(s) be held liable for any damages arising from
 *            the use of this software.
 * 
 *            Permission is granted to anyone to use this software for any purpose,
 *            including commercial applications, and to alter it and redistribute it
 *            freely, subject to the following restrictions:
 * 
 *            1. The origin of this software must not be misrepresented; you must not
 *            claim that you wrote the original software. If you use this software
 *            in a product, an acknowledgment in the product documentation would be
 *            appreciated but is not required.
 * 
 *            2. Altered source versions must be plainly marked as such, and must not
 *            be misrepresented as being the original software.
 * 
 *            3. This notice may not be removed or altered from any source distribution.
 * 
 * Description: 
 * Provides the user interface, and handles network/translation modules
 * 
 * Revision History:
 * 
 * Date(MMM/dd/yy)      Owner           Description
 * ----------------------------------------------------------------------------------------------------------------------------------------------
 * Oct/24/08            Kamran H. Khan  Added skeleton code
 * Oct/25/08            Kamran H. Khan  Added more methods
 *  
 * 
 * 
 * 
*/


using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;

namespace Maxima.Networker
{
    public class CNetMgr : Control
    {
        CServer m_theServer = null;
        CClient m_theClient = null;

        public delegate void StatusChangeEventHandler(string strData, bool bIsServer);
        public event StatusChangeEventHandler OnStatusChange;


        private bool m_bIsServer;
        public bool IsServer
        {
            get { return m_bIsServer; }
            set { m_bIsServer = value; }
        }

        private string m_strIP;
        [Category("Settings")]
        [Description("IP Address")]
        [BrowsableAttribute(true)]
        public string IP
        {
            get { return m_strIP; }
            set { m_strIP = value; }
        }

        private int m_nPort;

        [Category("Settings")]
        [Description("Port")]
        [BrowsableAttribute(true)]
        public int Port
        {
            get { return m_nPort; }
            set { m_nPort = value; }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="CNetMgr"/> class.
        /// </summary>
        /// <param name="strIP">The STR IP.</param>
        /// <param name="nPort">The n port.</param>
        /// <param name="bRunAsServer">if set to <c>true</c> [b run as server].</param>
        public CNetMgr(string strIP, int nPort, bool bRunAsServer)
        {
            m_bIsServer = bRunAsServer;
            m_nPort = nPort;
            m_strIP = strIP;


        }

        /// <summary>
        /// M_thes the client_ on status change.
        /// </summary>
        /// <param name="strData">The STR data.</param>
        void m_theClient_OnStatusChange(string strData)
        {
            if (OnStatusChange != null)
                OnStatusChange(strData, false);
        }

        /// <summary>
        /// M_thes the server_ on status change.
        /// </summary>
        /// <param name="strData">The STR data.</param>
        void m_theServer_OnStatusChange(string strData)
        {
            if (OnStatusChange != null)
                OnStatusChange(strData, true);
        }

        /// <summary>
        /// Starts this instance.
        /// </summary>
        public void Start()
        {
            if (m_bIsServer)
            {
                //Start server.
                m_theServer = new CServer(m_strIP, m_nPort);
                m_theServer.OnStatusChange += new CServer.StatusChangeEventHandler(m_theServer_OnStatusChange);
                m_theServer.Start();
            }
            else
            {
                m_theClient = new CClient(m_strIP, m_nPort);
                m_theClient.OnStatusChange += new CClient.StatusChangeEventHandler(m_theClient_OnStatusChange);
                m_theClient.Start();
            }
        }

        /// <summary>
        /// Stops this instance.
        /// </summary>
        public void Stop()
        {
            if (m_bIsServer && m_theServer != null)
            {
                m_theServer.Stop();
            }
            else
            {
                if (m_theClient != null)
                {
                    Send("logout");
                    m_theClient.Stop();
                }
            }

        }

        /// <summary>
        /// Determines whether the specified b server is connected.
        /// </summary>
        /// <param name="bServer">if set to <c>true</c> [b server].</param>
        /// <returns>
        /// 	<c>true</c> if the specified b server is connected; otherwise, <c>false</c>.
        /// </returns>
        public bool IsConnected(bool bServer)
        {
            if (bServer)
            {
                return m_theServer.IsConnected;
            }
            else
            {
                return m_theClient.IsConnected;
            }
        }

        /// <summary>
        /// Sends the specified STR data.
        /// </summary>
        /// <param name="strData">The STR data.</param>
        /// <returns></returns>
        public int Send(string strData)
        {
            int nBytesSent = 0;
            if (m_bIsServer)
            {
                nBytesSent = m_theServer.Send(strData);
            }
            else
            {
                nBytesSent = m_theClient.Send(strData);
            }

            return nBytesSent;
        }
    }
}
